package com.bfs.controller;

import cfk.pojo.order.*;
import com.alibaba.fastjson.JSONObject;
import com.bfs.service.cust.ICustServiceCfkClient;
import com.bfs.service.hystrix.collaspers.HystrixCollapseCommand;
import com.bfs.service.hystrix.collaspers.HystrixCommandSingle;
import com.bfs.service.hystrix.collaspers.QueryService;
import com.bfs.service.hystrix.collaspers.observable.Obserable;
import com.bfs.service.order.IOrderServiceCfkClient;
import com.bfs.service.wallet.IWalletServiceCfkClient;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rx.Observable;
import rx.Observer;

import java.util.List;
import java.util.concurrent.Future;

/**
 * @author baohuajie
 * @description: test
 * @date 2020/11/135:36 PM
 */
@RestController
@RequestMapping("/bfs")
public class OrderController {
    private final Logger log = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private IOrderServiceCfkClient iOrderServiceCfkClient;
    @Autowired
    private ICustServiceCfkClient iCustServiceCfkClient;
    @Autowired
    private IWalletServiceCfkClient iWalletServiceCfkClient;
    @Autowired
    private QueryService queryService;

    /**
     * 没有hystrix，在消费端定义接口，通过@RequestMapping进行路径匹配，进行调用远程服务
     * @return
     */
    @RequestMapping(value = "/order/queryLoanByLoanId")
    public OrderQueryResp queryLoanByLoanId(){
        log.info("=========当前端口号:{}=========");
        OrderQueryReq queryReq = new OrderQueryReq();
        queryReq.setLoanId(123123L);
        OrderQueryResp orderQueryResp = iOrderServiceCfkClient.queryLoanByLoanId(queryReq);
        return orderQueryResp;
    }
    /**
     * 没有hystrix，在消费端定义接口，继承服务端的接口，不需要通过@RequestMapping进行路径匹配
     * @return
     */
    @RequestMapping(value = "/cust/queryCustByCustId")
    public CustQueryResp queryCustByCustId(){
        log.info("=========当前端口号:{}=========");
        CustQueryReq custQueryReq = new CustQueryReq();
        custQueryReq.setCustId("12312");
        CustQueryResp custQueryResp = iCustServiceCfkClient.queryCustByCustId(custQueryReq);
        return custQueryResp;

    }

    /**
     * 有hystrix，在消费端定义接口，通过@RequestMapping进行路径匹配，进行调用远程服务
     * @return
     */
    @RequestMapping(value = "/wallet/queryWalletByCustId")
    public WalletQueryResp queryWalletByCustId(){
        log.info("=========当前端口号:{}=========");
        WalletQueryReq walletQueryReq = new WalletQueryReq();
        walletQueryReq.setCustId("12312");
        WalletQueryResp walletQueryResp = iWalletServiceCfkClient.queryWalletByCustId(walletQueryReq);
        return walletQueryResp;

    }

    /**
     * 有hystrix，在消费端定义接口，通过@RequestMapping进行路径匹配，进行调用远程服务
     * @return
     */
    @RequestMapping(value = "/histrix/histrixTest")
    public String histrixTest(@RequestBody CustQueryReq custQueryReq) throws Exception {
        log.info("=========连接线程:{}=========", Thread.currentThread().getName());
        String querySingle = null;
        try {
//        HystrixCommandSingle hystrixCommandSingle = new HystrixCommandSingle(queryService, custQueryReq.getCustId());
//        String execute = hystrixCommandSingle.execute();

            querySingle = queryService.querySingle(custQueryReq.getCustId());
            List<CustQueryResp> custQueryResps = queryService.queryBatch(custQueryReq.getCustId());
            List<CustQueryResp> custQueryResps1 = queryService.hystrixObservable(custQueryReq);
        } catch (Exception e) {
            log.info("=========执行catch=========");
        }
        return querySingle;

    }

    /**
     * 有hystrix，在消费端定义接口，通过@RequestMapping进行路径匹配，进行调用远程服务
     * @return
     */
    @RequestMapping(value = "/histrix/histrixTestBatch")
    public String histrixTestBatch(@RequestBody CustQueryReq custQueryReq) throws Exception {
        log.info("=========连接线程:{}=========", Thread.currentThread().getName());
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        HystrixCollapseCommand bc1  = new HystrixCollapseCommand(queryService, custQueryReq);
        HystrixCollapseCommand bc2  = new HystrixCollapseCommand(queryService, custQueryReq);
        HystrixCollapseCommand bc3  = new HystrixCollapseCommand(queryService, custQueryReq);
        HystrixCollapseCommand bc4  = new HystrixCollapseCommand(queryService, custQueryReq);
        HystrixCollapseCommand bc5  = new HystrixCollapseCommand(queryService, custQueryReq);

        Future<CustQueryResp> queue1 = bc1.queue();
        Future<CustQueryResp> queue2 = bc2.queue();
        Future<CustQueryResp> queue3 = bc3.queue();
        Future<CustQueryResp> queue4 = bc4.queue();
        Future<CustQueryResp> queue5 = bc5.queue();

        CustQueryResp resp1 = queue1.get();
        CustQueryResp resp2 = queue2.get();
        CustQueryResp resp3 = queue3.get();
        CustQueryResp resp4 = queue4.get();
        CustQueryResp resp5 = queue5.get();
//        CustQueryResp execute = hystrixCollapseCommand.execute();
//         return JSONObject.toJSONString(execute);s
        return "";

    }


    /**
     * 有hystrix，在消费端定义接口，通过@RequestMapping进行路径匹配，进行调用远程服务
     * @return
     */
    @RequestMapping(value = "/histrix/histrixObserable")
    public String histrixObserable(@RequestBody CustQueryReq custQueryReq) throws Exception {
        Obserable obserable = new Obserable(queryService);
        Observable construct = obserable.observe();
//        Observable construct = obserable.toObservable();
//        construct.subscribe(new Observer() {
//            @Override
//            public void onCompleted() {
//                System.out.println("完成");
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                System.out.println("异常");
//            }
//
//            @Override
//            public void onNext(Object o) {
//                if (o instanceof CustQueryResp[]) {
//                    String s = JSONObject.toJSONString(o);
//                    System.out.println(s);
//                } else if (o instanceof String) {
//                    System.out.println(o);
//                }
//            }
//        });
        return "";

    }
}
